package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.pojo.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.utils.common.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传图片素材
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        //检查参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //上传图片到minio中
        //用uuid生成文件名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");
        //获取原文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //截取原文件名的后缀
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = null;
        try {
            //上传图片文件到minio中获取url
             url = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
        } catch (IOException e) {
            log.error("上传图片失败");
            throw new RuntimeException(e);
        }

        //保存到数据库中
        WmMaterial wmMaterial = new WmMaterial();
        //从Threadlocal中获取userid
        wmMaterial.setUserId(UserThreadLocal.getUserId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        //调用save方法保存数据库
        this.save(wmMaterial);
        //返回结果
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表查询
     *
     * @param wmMaterialDto
     * @return
     */
    @Override
    public PageResponseResult list(WmMaterialDto wmMaterialDto) {
        //1.检查参数
        wmMaterialDto.checkParam();
        //2.分页查询
        Integer page = wmMaterialDto.getPage();
        Integer size = wmMaterialDto.getSize();
        IPage<WmMaterial> pageInfo = new Page(page,size);
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //是否收藏
        if (wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() == 1){
            queryWrapper.eq(WmMaterial ::getIsCollection,wmMaterialDto.getIsCollection());
        }
        //按照用户查询
        queryWrapper.eq(WmMaterial::getUserId,UserThreadLocal.getUserId());
        //按照时间倒序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //调用page方法分页查询
        IPage<WmMaterial> wmMaterialIPage = this.page(pageInfo, queryWrapper);
        long total = wmMaterialIPage.getTotal();
        List<WmMaterial> records = wmMaterialIPage.getRecords();
        PageResponseResult pageResponse = new PageResponseResult();
        pageResponse.setTotal(total);
        pageResponse.setData(records);

        //3.结果返回
        return pageResponse;
    }
}
